﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using MINERP.MODEL.GoodsDelivery;
using MINERP.DAL.GoodsDelivery;

namespace MINERP.BLL.GoodsDelivery
{
    public class GoodsDeliveryBLL
    {
        //参数
        private DAL.GoodsDelivery.GoodsDeliveryDAL dal = new DAL.GoodsDelivery.GoodsDeliveryDAL();
        #region 获取所有未发送的账单
        /// <summary>
        /// 获取所有未发送的账单
        /// </summary>
        /// <returns></returns>
        public List<MODEL.GoodsDelivery.BillInfo> GetNoDeliveryBill(out DataTable dt)
        {
            try
            {
                dt = dal.GetNoDeliveryBill();
                return ChangeDTToList(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将DT转化成List
        /// <summary>
        /// 将DT转化成List
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<MODEL.GoodsDelivery.BillInfo> ChangeDTToList(DataTable dt)
        {
            try
            {
                List<MODEL.GoodsDelivery.BillInfo> billInfos = new List<MODEL.GoodsDelivery.BillInfo>();
                foreach (DataRow row in dt.Rows)
                {
                    MODEL.GoodsDelivery.BillInfo bill = new MODEL.GoodsDelivery.BillInfo();
                    bill.BillId = Convert.ToInt32(row["BillId"]);
                    bill.BillOrderCode = row["BillOrderCode"].ToString();
                    bill.BillCreateTime = Convert.ToDateTime(row["BillCreateTime"]);
                    bill.BusTimetable = row["BusTimetable"].ToString();
                    bill.ConsigneeName = row["ConsigneeName"].ToString();
                    bill.ConsigneeTelePhone = row["ConsigneeTelePhone"].ToString();
                    bill.DistributionAddress = row["DistributionAddress"].ToString();
                    bill.DistributionMode = row["DistributionMode"].ToString();
                    bill.FarePayer = row["FarePayer"].ToString();
                    bill.Remark = row["Remark"].ToString();
                    bill.ShopId = Convert.ToInt32(row["ShopId"]);
                    bill.ShopName = row["ShopName"].ToString();
                    bill.Salesman = row["Salesman"].ToString();
                    bill.Area = row["Area"].ToString();
                    bill.ProductNum = Convert.ToInt32(row["ProductNum"]);
                    bill.TotalPrice = Convert.ToDouble(row["TotalPrice"]);
                    billInfos.Add(bill);
                }
                return billInfos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public List<MODEL.GoodsDelivery.BillInfo> ChangeRowsToList(DataRow[] rows)
        {
            try
            {
                List<MODEL.GoodsDelivery.BillInfo> bills = new List<MODEL.GoodsDelivery.BillInfo>();
                foreach (DataRow row in rows)
                {
                    MODEL.GoodsDelivery.BillInfo bill = new MODEL.GoodsDelivery.BillInfo();
                    bill.BillId = Convert.ToInt32(row["BillId"]);
                    bill.BillOrderCode = row["BillOrderCode"].ToString();
                    bill.BillCreateTime = Convert.ToDateTime(row["BillCreateTime"]);
                    bill.BusTimetable = row["BusTimetable"].ToString();
                    bill.ConsigneeName = row["ConsigneeName"].ToString();
                    bill.ConsigneeTelePhone = row["ConsigneeTelePhone"].ToString();
                    bill.DistributionAddress = row["DistributionAddress"].ToString();
                    bill.DistributionMode = row["DistributionMode"].ToString();
                    bill.FarePayer = row["FarePayer"].ToString();
                    bill.Remark = row["Remark"].ToString();
                    bill.ShopId = Convert.ToInt32(row["ShopId"]);
                    bill.ShopName = row["ShopName"].ToString();
                    bill.Salesman = row["Salesman"].ToString();
                    bill.Area = row["Area"].ToString();
                    bill.ProductNum = Convert.ToInt32(row["ProductNum"]);
                    bill.TotalPrice = Convert.ToDouble(row["TotalPrice"]);
                    bills.Add(bill);
                }
                return bills;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 将DT转化成List
        /// <summary>
        /// 将DT转化成List
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<MODEL.GoodsDelivery.BillInfo> ChangeDTToListStatus(DataTable dt)
        {
            try
            {
                List<MODEL.GoodsDelivery.BillInfo> billInfos = new List<MODEL.GoodsDelivery.BillInfo>();
                foreach (DataRow row in dt.Rows)
                {
                    MODEL.GoodsDelivery.BillInfo bill = new MODEL.GoodsDelivery.BillInfo();
                    bill.DeliverId = Convert.ToInt32(row["DeliverId"]);
                    bill.BillId = Convert.ToInt32(row["BillId"]);
                    bill.BillOrderCode = row["BillOrderCode"].ToString();
                    bill.BillCreateTime = Convert.ToDateTime(row["BillCreateTime"]);
                    bill.BusTimetable = row["BusTimetable"].ToString();
                    bill.ConsigneeName = row["ConsigneeName"].ToString();
                    bill.ConsigneeTelePhone = row["ConsigneeTelePhone"].ToString();
                    bill.DistributionAddress = row["DistributionAddress"].ToString();
                    bill.DistributionMode = row["DistributionMode"].ToString();
                    bill.FarePayer = row["FarePayer"].ToString();
                    bill.Remark = row["Remark"].ToString();
                    bill.ShopId = Convert.ToInt32(row["ShopId"]);
                    bill.ShopName = row["ShopName"].ToString();
                    bill.ProductNum = Convert.ToInt32(row["ProductNum"]);
                    bill.TotalPrice = Convert.ToDouble(row["TotalPrice"]);
                    billInfos.Add(bill);
                }
                return billInfos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取数据库中最大的送货单Id
        /// <summary>
        /// 获取数据库中最大的送货单Id
        /// </summary>
        /// <returns></returns>
        public int GetMaxDeliveryId()
        {
            try
            {
                return dal.GetMaxDeliveryId();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 插入未打印的发货信息
        /// <summary>
        /// 插入未打印的发货信息
        /// </summary>
        /// <param name="billInfos"></param>
        /// <param name="deliverId"></param>
        public void InsertDeliveryNoPrint(List<MODEL.GoodsDelivery.BillInfo> billInfos,int deliverId)
        {
            try
            {
                dal.InsertDeliveryNoPrint(billInfos, deliverId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 按状态，发货单Id查询未发送的账单
        /// <summary>
        /// 按状态，发货单Id查询未发送的账单
        /// </summary>
        /// <param name="deliveId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<MODEL.GoodsDelivery.BillInfo> GetNoDeliveryBillByStatus(int deliveId, int status)
        {
            try
            {
                DataTable dt = dal.GetNoDeliveryBillByStatus(deliveId, status);
                return ChangeDTToListStatus(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 按状态查询未发送的账单
        /// <summary>
        /// 按状态查询未发送的账单
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<MODEL.GoodsDelivery.BillInfo> GetNoDeliveryBillByStatusOnly(int status)
        {
            try
            {
                DataTable dt = dal.GetNoDeliveryBillByStatusOnly(status);
                return ChangeDTToListStatus(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 从发货单表删除指定的账单
        /// <summary>
        /// 从发货单表删除指定的账单
        /// </summary>
        /// <param name="DelBills"></param>
        public void DelBillFromDeliveryInfo(List<MODEL.GoodsDelivery.BillInfo> DelBills)
        {
            try
            {
                dal.DelBillFromDeliveryInfo(DelBills);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        /// <summary>
        /// 创建发货单数据源
        /// </summary>
        /// <param name="printBills"></param>
        public DataSet GetPrintData(List<MODEL.GoodsDelivery.BillInfo> printBills,DataTable deliveryDT)
        {
            try
            {
                List<MODEL.GoodsDelivery.ShopInfo> shopInfos = new List<MODEL.GoodsDelivery.ShopInfo>();
                DataTable shopDT = CreateShopInfoDT();
                DataTable billDT = CreateBillDT();
                var shopGroups = printBills.GroupBy(p => p.ShopId);
                foreach (var shopGroup in shopGroups)
                {
                    DataRow row = shopDT.NewRow();
                    row["ShopId"] = shopGroup.First().ShopId;
                    row["ShopName"] = shopGroup.First().ShopName;
                    row["ConsigneeName"] = shopGroup.First().ConsigneeName; 
                    row["ConsigneeTelePhone"] =shopGroup.First().ConsigneeTelePhone; 
                    row["DistributionMode"] =shopGroup.First().DistributionMode; 
                    row["DistributionAddress"] = shopGroup.First().DistributionAddress;
                    row["FarePayer"] =  shopGroup.First().FarePayer;
                    row["BusTimetable"] = shopGroup.First().BusTimetable;
                    row["Remark"] = shopGroup.First().Remark;
                    row["BillCount"] = shopGroup.Count();
                    row["Area"] = shopGroup.First().Area;       //地区
                    shopDT.Rows.Add(row);
                }
                foreach (MODEL.GoodsDelivery.BillInfo bill in printBills)               //账单状态
                {
                    DataRow row = billDT.NewRow();
                    row["BillId"] = bill.BillId;
                    row["ShopId"] = bill.ShopId;
                    row["BillOrderCode"] = bill.BillOrderCode;
                    row["ProductNum"] = bill.ProductNum;
                    row["TotalPrice"] = bill.TotalPrice;
                    billDT.Rows.Add(row);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(billDT);
                billDT.TableName = "billDT";
                ds.Tables.Add(deliveryDT);
                deliveryDT.TableName = "deliveryDT";
                ds.Tables.Add(shopDT);
                shopDT.TableName = "shopDT";
                ds.WriteXml(@"C:\Windows\xmldata\DeliveryInfo.xml", XmlWriteMode.WriteSchema);      //发货信息xml
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 获取运输部员工列表
        /// <summary>
        /// 获取运输部员工列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetDeliveryWorkers()
        {
            try
            {
                return dal.GetDeliveryWorkers();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改发货单状态
        /// <summary>
        /// 修改发货单状态
        /// </summary>
        /// <param name="status"></param>
        /// <param name="deliverId"></param>
        public void ChangeDeliveryStatus(int status, int deliverId,int userId)
        {
            try
            {
                dal.ChangeDeliverStatus(status, deliverId,userId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        /// <summary>
        /// 将账单置为不需要发货
        /// </summary>
        /// <param name="billIds"></param>
        public void DelNeedDeliver(List<int> billIds)
        {
            try
            {
                dal.DelNeedDeliver(billIds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 发货的商户信息结构
        /// <summary>
        /// 发货的商户信息结构
        /// </summary>
        /// <returns></returns>
        public DataTable CreateShopInfoDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ShopId",typeof(int)));
            dt.Columns.Add(new DataColumn("ShopName",typeof(string)));
            dt.Columns.Add(new DataColumn("ConsigneeName",typeof(string)));
            dt.Columns.Add(new DataColumn("ConsigneeTelePhone",typeof(string)));
            dt.Columns.Add(new DataColumn("DistributionMode",typeof(string)));
            dt.Columns.Add(new DataColumn("DistributionAddress",typeof(string)));
            dt.Columns.Add(new DataColumn("FarePayer",typeof(string)));
            dt.Columns.Add(new DataColumn("BusTimetable",typeof(string)));
            dt.Columns.Add(new DataColumn("Remark",typeof(string)));
            dt.Columns.Add(new DataColumn("BillCount",typeof(int)));
            dt.Columns.Add(new DataColumn("Area", typeof(string)));                     //地区
            return dt;
        }
        #endregion

        #region 账单概述结构
        /// <summary>
        /// 账单概述结构
        /// </summary>
        /// <returns></returns>
        public DataTable CreateBillDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("BillId", typeof(int)));
            dt.Columns.Add(new DataColumn("ShopId", typeof(int)));
            dt.Columns.Add(new DataColumn("CreateTime", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("BillOrderCode", typeof(int)));
            dt.Columns.Add(new DataColumn("ProductNum", typeof(int)));                  //账单内的产品数量
            dt.Columns.Add(new DataColumn("TotalPrice", typeof(double)));
            return dt;
        }
        #endregion

        #region 发货单总体信息结构
        /// <summary>
        /// 发货的发货单信息结构
        /// </summary>
        /// <returns></returns>
        public DataTable CreateDeliveryDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("DeliveryId", typeof(int)));
            dt.Columns.Add(new DataColumn("UserName", typeof(int)));
            dt.Columns.Add(new DataColumn("Remark", typeof(int)));
            return dt;
        }
        #endregion
    }
}
